package com.example.myapplication.common;

import java.io.Serializable;

public class Res<T> implements Serializable {

    private static final long serialVersionUID = -4759561292079610977L;
    private int code;
    private String msg;
    private T data;

    public static <T> Res<T> ok() {
        return build(ResEnum.SUCCESS.getValue(), ResEnum.SUCCESS.getDesc(), null);
    }

    public static <T> Res<T> ok(T data) {
        return build(ResEnum.SUCCESS.getValue(), ResEnum.SUCCESS.getDesc(), data);
    }

    public static <T> Res<T> fail() {
        return build(ResEnum.Fail.getValue(), ResEnum.Fail.getDesc(), null);
    }

    public static <T> Res<T> fail(String msg) {
        return build(ResEnum.Fail.getValue(), msg, null);
    }

    public static <T> Res<T> fail(ResEnum resEnum) {
        return build(resEnum.getValue(), resEnum.getDesc(), null);
    }

    public static <T> Res<T> fail(ResEnum resEnum, String msg) {
        return build(resEnum.getValue(), msg, null);
    }

    public static <T> Res<T> fail(int code, String msg) {
        return build(code, msg, null);
    }

    private static <T> Res<T> build(int code, String msg, T data) {
        Res<T> res = new Res<T>();
        res.setCode(code);
        res.setMsg(msg);
        res.setData(data);
        return res;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}